package org.acegisecurity.util.expressioninterpreter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Class representing the Or operator. The or operator is a binary operator so it is applied
 * to two values from the value {@link Stack}.
 *
 * @author Jamie Craane
 * @author Ivo Woltring
 */
public class OrOperator extends AbstractOperator {
    private static final Log log = LogFactory.getLog(OrOperator.class);
    public static final String OR_OPERATOR = "OR";

    /**
     * @see Operator#apply(String[], Stack)
     */
    public boolean apply(String[] roles, Stack valueStack) {
        final String role_1 = (String) valueStack.pop();
        final String role_2 = (String) valueStack.pop();
        boolean r1 = hasRole(roles, role_1);
        boolean r2 = hasRole(roles, role_2);
        boolean accessGranted = r2 || r1;
        log.debug("Evaluated [" + role_2 + "] OR [" + role_1 + "] to [" + accessGranted + "]");
        super.pushResultToValueStack(accessGranted, valueStack);

        return accessGranted;
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        return OR_OPERATOR;
    }

    /**
     * @see Operator#hasGreaterPrecedenceThan(Operator)
     */
    public boolean hasGreaterPrecedenceThan(Operator thisOp) {
        return false;
    }
}
